﻿/******************************************************************
Module Name:  MessageBox Javascript Code
Created By:   Justin Jiang
Created Date: 2008-11-29
Description:
    1. Define MessageBoxButtons collection
    2. Define MessageBoxIcon collection
    3. Define DefaultMessageBoxButton collection
******************************************************************/

//  Specifies constants defining which buttons 
function MessageBoxButtons(){}

// The message box contains an OK button.
MessageBoxButtons.OK                = 0;

// The message box contains OK and Cancel buttons.
MessageBoxButtons.OKCancel          = 1;

// The message box contains Abort, Retry, and Ignore buttons.
MessageBoxButtons.AbortRetryIgnore  = 2;

// The message box contains Yes, No, and Cancel buttons.
MessageBoxButtons.YesNoCancel       = 3;

// The message box contains Yes and No buttons.
MessageBoxButtons.YesNo             = 4;

// The message box contains Retry and Cancel buttons.
MessageBoxButtons.RetryCancel       = 5;

//  Specifies constants defining which information to display.
function MessageBoxIcon(){}

// The message box contain no symbols.
MessageBoxIcon.None                 = 0;

// The message box contains a symbol consisting of white X in a circle
// with a red background.
MessageBoxIcon.Error                = 16;

// The message box contains a symbol consisting of white X in a circle
// with a red background.
MessageBoxIcon.Hand                 = 16;

// The message box contains a symbol consisting of white X in a circle
// with a red background.
MessageBoxIcon.Stop                 = 16;

// The message box contains a symbol consisting of a question mark in
// a circle.
MessageBoxIcon.Question             = 32;

// The message box contains a symbol consisting of an exclamation point
// in a triangle with a yellow background.
MessageBoxIcon.Exclamation          = 48;

// The message box contains a symbol consisting of an exclamation point
// in a triangle with a yellow background.
MessageBoxIcon.Warning              = 48;

// The message box contains a symbol consisting of a lowercase letter i
// in a circle.
MessageBoxIcon.Information          = 64;

// The message box contains a symbol consisting of a lowercase letter i
// in a circle.
MessageBoxIcon.Asterisk             = 64;

// Specifies constants defining which button to choose as default.
function DefaultMessageBoxButton(){}

// The first button as default button.
DefaultMessageBoxButton.Button1     = 0;

// The second button as default button.
DefaultMessageBoxButton.Button2     = 1;

// The third button as default button.
DefaultMessageBoxButton.Button3     = 3;


// Specifies constants definition dialog return value
function DialogResult(){}

// None thing return
DialogResult.None                   = 0;

// OK button clicked
DialogResult.OK                     = 1;

// Cancel button clicked
DialogResult.Cancel                 = 2;

// Yes button clicked
DialogResult.Yes                    = 1;

// No button clicked
DialogResult.No                     = 2;

// Abort button clicked
DialogResult.Abort                  = 3;

// Retry button clicked
DialogResult.Retry                  = 4;

// Ignore button clicked
DialogResult.Ignore                 = 5;

// Define MessageBox object
function MessageBox(){}

// Set the relative path
MessageBox.RelativePath = "";

// Set whether encode message
MessageBox.EncodeHtml = true;

// Get the dialog result
MessageBox.DiaglogResult = DialogResult.None;

// Pixals, Margin
MessageBox._charPixal = 8;
MessageBox._hMargin = 0;
MessageBox._vMargin = 4;

// Show Message Box
// text: Text to display as message
// caption: Text to display as caption
// buttons: Buttons display on the form
// defaultButton: The default focused button
MessageBox.Show = function(text, caption, buttons, icon, defaultButton) {
    // Construct DialogArguments
    var arguments = new Object();
    arguments.Text = text == undefined ? "" : text;
    arguments.Caption = caption == undefined ? "" : caption;
    arguments.MessageBoxButton =
        buttons == undefined ? MessageBoxButtons.OK : buttons;
    arguments.MessageBoxIcon =
        icon == undefined ? MessageBoxIcon.Information : icon;
    arguments.DefaultButton =
        defaultButton == undefined ? DefaultMessageBoxButton.Button1 : defaultButton;
    arguments.EncodeHtml = MessageBox.EncodeHtml;

    // Show modal dialog
    var width = calculateWinWidth(text);
    var height = calculateWinHeight(text);
    
    var result = window.showModalDialog(MessageBox.RelativePath + "Message/MessageBox.htm",
        arguments,
        "dialogWidth: "
            + width
            + "px; dialogHeight: "
            + height
            + "px; resizable: no; status: no; scroll: no");
    if (result == undefined) {
        MessageBox.DiaglogResult = DialogResult.None;
    }
    else {
        MessageBox.DiaglogResult = result;
    }
    return MessageBox.DiaglogResult;
}

MessageBox.AsncShow = function(text, caption, buttons, icon, defaultButton, delayInterval)
{
    var divHide_MessageBox = document.createElement("DIV");
    var frmHide_MessageBox = document.createElement("IFRAME");
    divHide_MessageBox.id = "divAsyncMessageBoxHide";
    divHide_MessageBox.style.display = "none";
    frmHide_MessageBox.src = MessageBox.RelativePath + "Message/AsyncMessageBox.htm?Text=" + text + "&Caption=" + caption + "&Buttons=" + buttons + "&Icon=" + icon + "&DefaultButton=" + defaultButton + "&DelayInterval=" + delayInterval;
    divHide_MessageBox.appendChild(frmHide_MessageBox);
    document.body.insertBefore(divHide_MessageBox, document.body.getElementsByTagName("FORM")[0]);
}

// Calculate width
function calculateWinWidth(text)
{
    var rowCharsCount = 20;
    var otherPixs = 50;
    var i = 0;
    var j = 0;
    for(i = 0; i < text.length; i++)
    {
        if (text.charAt(i) == "\n")
        {
            if (rowCharsCount < j)
            {
                rowCharsCount = j;
                j = 0;
            }
        }
        j++;
        if (text.charAt(i).toLowerCase() == "w" ||
            text.charAt(i).toLowerCase() == "m")
        {
            j = j + 0.2;
        }
    }
    if (rowCharsCount < j)
    {
        rowCharsCount = j;
    }
    return rowCharsCount * (MessageBox._charPixal + MessageBox._hMargin) + otherPixs;
}

// Calculate height
function calculateWinHeight(text)
{
    var linesCount = 5;
    var otherPixs = 70;
    var i = 0;
    var j = 1;
    for(i = 0; i < text.length; i++)
    {
        if (text.charAt(i) == "\n")
        {
            j++;
        }
    }
    if (j > linesCount)
    {
        linesCount = j;
    }
    return linesCount * (MessageBox._charPixal + MessageBox._vMargin) + otherPixs;
}

/********************** Creat MessageBox Content ************************/
// On MessageBox load
function onMessageBoxLoad()
{
    setCaption(window.dialogArguments.Caption);
    setText(window.dialogArguments.Text, window.dialogArguments.EncodeHtml);
    setButtons(window.dialogArguments.MessageBoxButton,
        window.dialogArguments.DefaultButton);
    setIcon(window.dialogArguments.MessageBoxIcon);
}

// Set caption
function setCaption(caption)
{
    if (caption != undefined)
    {
        document.title = caption;
    }
}

// Set message
function setText(text, encode)
{
    var tdText = document.getElementById("tdText");
    if (tdText == undefined)
    {
        return;
    }
    
    var html = text;
    if (encode)
    {
        html = $EncodeHtml(text);
    }
    tdText.innerHTML = html;
}

// Set icon
function setIcon(icon)
{
    var msgIcon = icon;
    if (icon == undefined)
    {
        msgIcon = MessageBoxIcon.None;
    }
    
    var tdIcon = document.getElementById("tdIcon");
    if (tdIcon == undefined)
    {
        return;
    }
    
    switch(msgIcon)
    {
        case MessageBoxIcon.Error:
        case MessageBoxIcon.Stop:
        case MessageBoxIcon.Hand:
        {
            tdIcon.appendChild($CTag("img", "src=alert_error.gif", "border=0", "align=absmiddle", "style=background-color: Transparent; width: 32px;"));
            break;
        }
        case MessageBoxIcon.Question:
        {
            tdIcon.appendChild($CTag("img", "src=alert_question.gif", "border=0", "align=absmiddle", "style=background-color: Transparent"));
            break;
        }
        case MessageBoxIcon.Exclamation:
        case MessageBoxIcon.Warning:
        {
            tdIcon.appendChild($CTag("img", "src=alert_warn.gif", "border=0", "align=absmiddle", "style=background-color: Transparent"));
            break;
        }
        case MessageBoxIcon.Information:
        case MessageBoxIcon.Asterisk:
        {
            tdIcon.appendChild($CTag("img", "src=alert_info.gif", "border=0", "align=absmiddle", "style=background-color: Transparent"));
            break;
        }
        default:
            break;
    }
}

// Set buttons
function setButtons(buttons, defaultButton)
{
    var msgButtons = buttons;
    var dftButton = defaultButton;
    if (buttons == undefined)
    {
        msgButtons = MessageBoxButtons.OK;
    }
    if (dftButton == undefined)
    {
        dftButton = DefaultMessageBoxButton.Button1;
    }
    
    var tdButtons = document.getElementById("tdButtons");
    if (tdButtons == undefined)
    {
        return;
    }
    
    switch(msgButtons)
    {
       case MessageBoxButtons.OKCancel:
       {
            var btnOK = $CTag("INPUT", "id=btnOK", "type=button", "value=OK");
            $AddHandler(btnOK, "onclick", OnOK_Click);
            tdButtons.appendChild(btnOK);
            
            var btnCancel = $CTag("INPUT", "id=btnCancel", "type=button", "value=Cancel");
            $AddHandler(btnCancel, "onclick", OnCancel_Click);
            tdButtons.appendChild(btnCancel);
            
            if (dftButton == DefaultMessageBoxButton.Button2)
            {
                btnCancel.focus();
            }
            else
            {
                btnOK.focus();
            }
            break;
        }
        case MessageBoxButtons.AbortRetryIgnore:
        {
            var btnRetry = $CTag("INPUT", "id=btnRetry", "type=button", "value=Retry");
            $AddHandler(btnRetry, "onclick", OnRetry_Click);
            tdButtons.appendChild(btnRetry);
            
            var btnAbort = $CTag("INPUT", "id=btnAbort", "type=button", "value=Abort");
            $AddHandler(btnAbort, "onclick", OnAbort_Click);
            tdButtons.appendChild(btnAbort);
            
            var btnIgnore = $CTag("INPUT", "id=btnIgnore", "type=button", "value=Ignore");
            $AddHandler(btnIgnore, "onclick", OnIgnore_Click);
            tdButtons.appendChild(btnIgnore);
            
            if (dftButton == DefaultMessageBoxButton.Button2)
            {
                btnAbort.focus();
            }
            else if (dftButton == DefaultMessageBoxButton.Button3)
            {
                btnIgnore.focus();
            }
            else
            {
                btnRetry.focus();
            }
            break;
        }
        case MessageBoxButtons.YesNoCancel:
        {
            var btnYes = $CTag("INPUT", "id=btnYes", "type=button", "value=Yes");
            $AddHandler(btnYes, "onclick", OnYes_Click);
            tdButtons.appendChild(btnYes);
            
            var btnNo = $CTag("INPUT", "id=btnNo", "type=button", "value=No");
            $AddHandler(btnNo, "onclick", OnNo_Click);
            tdButtons.appendChild(btnNo);
            
            var btnCancel = $CTag("INPUT", "id=btnCancel", "type=button", "value=Cancel");
            $AddHandler(btnCancel, "onclick", OnCancel_Click);
            tdButtons.appendChild(btnCancel);
            
            if (dftButton == DefaultMessageBoxButton.Button2)
            {
                btnNo.focus();
            }
            else if (dftButton == DefaultMessageBoxButton.Button3)
            {
                btnCancel.focus();
            }
            else
            {
                btnYes.focus();
            }
            break;
        }
        case MessageBoxButtons.YesNo:
        {
            var btnYes = $CTag("INPUT", "id=btnYes", "type=button", "value=Yes");
            $AddHandler(btnYes, "onclick", OnYes_Click);
            tdButtons.appendChild(btnYes);
            
            var btnNo = $CTag("INPUT", "id=btnNo", "type=button", "value=No");
            $AddHandler(btnNo, "onclick", OnNo_Click);
            tdButtons.appendChild(btnNo);
            
            if (dftButton == DefaultMessageBoxButton.Button2)
            {
                btnNo.focus();
            }
            else
            {
                btnYes.focus();
            }
            break;
        }
        case MessageBoxButtons.RetryCancel:
        {
            var btnRetry = $CTag("INPUT", "id=btnRetry", "type=button", "value=Retry");
            $AddHandler(btnRetry, "onclick", OnRetry_Click);
            tdButtons.appendChild(btnRetry);
            
            var btnCancel = $CTag("INPUT", "id=btnCancel", "type=button", "value=Cancel");
            $AddHandler(btnCancel, "onclick", OnCancel_Click);
            tdButtons.appendChild(btnCancel);
            
            if (dftButton == DefaultMessageBoxButton.Button2)
            {
                btnCancel.focus();
            }
            else
            {
                btnRetry.focus();
            }
            break;
        }
        default:
        {
            var btnOK = $CTag("INPUT", "id=btnOK", "type=button", "value=OK");
            $AddHandler(btnOK, "onclick", OnOK_Click);
            tdButtons.appendChild(btnOK);
            btnOK.focus();
            break;
       }
    }
}

// On OK button click
function OnOK_Click()
{
    window.returnValue = DialogResult.OK;
    window.close();
}

// On Cancel button click
function OnCancel_Click()
{
    window.returnValue = DialogResult.Cancel;
    window.close();
}

// On Retry button click
function OnRetry_Click()
{
    window.returnValue = DialogResult.Retry;
    window.close();
}

// On Abort button click
function OnAbort_Click()
{
    window.returnValue = DialogResult.Abort;
    window.close();
}

// On Abort button click
function OnIgnore_Click()
{
    window.returnValue = DialogResult.Ignore;
    window.close();
}

// On Abort button click
function OnYes_Click()
{
    window.returnValue = DialogResult.Yes;
    window.close();
}

// On Abort button click
function OnNo_Click()
{
    window.returnValue = DialogResult.No;
    window.close();
}

/***********************  HTML Helper Functions *************************/
// Create HTML element
// tagName: HTML Element TagName
// others: Key Value Pair
function $CTag(tagName)
{
    if (tagName == undefined)
    {
        return undefined;
    }
    
    // Create HTML element
    var element = document.createElement(tagName);
    
    var i = 0;
    // Add attributes
    for(i = 1; i < arguments.length; i++)
    {
        var keyValue = arguments[i].split("=");
        if (keyValue.length != 2)
        {
            continue;
        }
        
        $SetAttr(element, keyValue[0], keyValue[1]);
    }
    return element;
}

// Set object attribute
function $SetAttr(element, attr, value)
{
    if (element == undefined)
    {
        return "";
    }
    
    element.setAttribute(attr, value);
}

// Get object attribte
function $GetAttr(element, attr)
{
    if (element == undefined)
    {
        return "";
    }
    
    return element.getAttribute(attr);
}

function $EncodeHtml(html)
{
    var encodedHtml = html.replace(/>/g,"&gt;");
    encodedHtml = encodedHtml.replace(/</g,"&lt;");
    encodedHtml = encodedHtml.replace(/\"/g,"&quot;");
    encodedHtml = encodedHtml.replace(/\'/g,"&acute;");
    encodedHtml = encodedHtml.replace(/\n/g,"<br/>");
    encodedHtml = encodedHtml.replace(/\s/g,"&nbsp;");
    return encodedHtml;
}

// Attach event
function $AddHandler(element, eventName, fun)
{
    if (element == undefined)
    {
        return;
    }
    if (window.attachEvent)
    {
        //Support IE
        element.attachEvent(eventName, fun);
    }
    else
    {
        //Support FF
        element.addEventListener(eventName, fun, false);
    }
}